﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Sms
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Sms;
    using LdCms.IDAL.Sms;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;

    public partial class TencentPlatformService:BaseService<Ld_Sms_TencentPlatform>,ITencentPlatformService
    {
        private readonly ITencentPlatformDAL TencentPlatformDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public TencentPlatformService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, ITencentPlatformDAL TencentPlatformDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.TencentPlatformDAL = TencentPlatformDAL;
            this.Dal = TencentPlatformDAL;
        }
        public override void SetDal()
        {
            Dal = TencentPlatformDAL;
        }

        public bool SaveTencentPlatform(Ld_Sms_TencentPlatform entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                bool isDefault = entity.IsDefault.ToBool();
                bool state = entity.State.ToBool();
                if (string.IsNullOrEmpty(platformId))
                {
                    var primarykey = PrimaryKeyHelper.PrimaryKeyType.SmsTencentPlatform;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createPlatformId = PrimaryKeyHelper.MakePrimaryKey(primarykey, primaryKeyLen);
                    entity.PlatformID = createPlatformId;
                }
                entity.IsDefault = isDefault;
                entity.State = state;
                entity.CreateDate = DateTime.Now;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (isDefault)
                        {
                            var expression = ExtLinq.True<Ld_Sms_TencentPlatform>();
                            expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                            var lists = FindList(expression).ToList();
                            lists.ForEach(m => m.IsDefault = false);
                            dbContext.Update(lists);
                        }
                        dbContext.Add(entity);
                        intnum = db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                    }
                    return intnum > 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateTencentPlatform(Ld_Sms_TencentPlatform entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                bool isDefault = entity.IsDefault.ToBool();
                bool state = entity.State.ToBool();
                entity.IsDefault = isDefault;
                entity.State = state;
                entity.CreateDate = DateTime.Now;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (isDefault)
                        {
                            var expression = ExtLinq.True<Ld_Sms_TencentPlatform>();
                            expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID != platformId);
                            var lists = FindList(expression).ToList();
                            lists.ForEach(m => m.IsDefault = false);
                            dbContext.Update(lists);
                        }
                        dbContext.Update(entity);
                        intnum = db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                    }
                    return intnum > 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateTencentPlatformDefault(int systemId, string companyId, string platformId, bool isDefault)
        {
            try
            {
                var entity = Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
                if (entity == null)
                    throw new Exception("platform id invalid！");
                entity.IsDefault = isDefault;
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (isDefault)
                        {
                            var expression = ExtLinq.True<Ld_Sms_TencentPlatform>();
                            expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID != platformId);
                            var lists = FindList(expression).ToList();
                            lists.ForEach(m => m.IsDefault = false);
                            dbContext.Update(lists);
                        }
                        dbContext.Update(entity);
                        intnum = db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                    }
                    return intnum > 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateTencentPlatformState(int systemId, string companyId, string platformId, bool state)
        {
            try
            {
                var entity = Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
                if (entity == null)
                    throw new Exception("platform id invalid！");
                entity.State = state;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteTencentPlatform(int systemId, string companyId, string platformId)
        {
            try
            {
                return Delete(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_TencentPlatform GetTencentPlatform(int systemId, string companyId, string platformId)
        {
            try
            {
                return Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_TencentPlatform GetTencentPlatformByDefault(int systemId, string companyId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_TencentPlatform>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.IsDefault == true);
                return FindList(expression, m => m.CreateDate, true).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_TencentPlatform> GetTencentPlatformAll(int systemId, string companyId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_TencentPlatform>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                return FindList(expression, m => m.IsDefault, false).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountTencentPlatform(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}
